import 'package:meta/meta.dart';

/// Contains detailed placemark information.
@immutable
class Placeinfo {
  /// Constructs an instance with the given values for testing. [Placemark]
  /// instances constructed this way won't actually reflect any real information
  /// from the platform, just whatever was passed in at construction time.
  const Placeinfo(Map<dynamic, dynamic> map, {
    this.name,
    this.country,
    this.province,
    this.city,
    this.district,
    this.township,
    this.street,
  });

  const Placeinfo._({
    this.name,
    this.country,
    this.province,
    this.city,
    this.district,
    this.township,
    this.street,
  });

  /// The name associated with the placemark.
  final String? name;
  /// The name of the country associated with the placemark.
  final String? country;

  final String? province;
  final String? city;
  final String? district;
  final String? township;

  /// The street associated with the placemark.
  final String? street;

  @override
  bool operator ==(dynamic other) =>
      other is Placeinfo &&
      other.name == name &&
      other.country == country &&
      other.province == province &&
      other.city == city &&
      other.district == district &&
      other.township == township &&
      other.street == street;

  @override
  int get hashCode =>
      name.hashCode ^
      country.hashCode ^
      province.hashCode ^
      city.hashCode ^
      district.hashCode ^
      township.hashCode ^
      street.hashCode;

  /// Converts a list of [Map] instances to a list of [Placemark] instances.
  static List<Placeinfo> fromMaps(dynamic message) {
    if (message == null) {
      throw ArgumentError('The parameter \'message\' should not be null.');
    }

    final List<Placeinfo> list = message.map<Placeinfo>(fromMap).toList();
    return list;
  }

  /// Converts the supplied [Map] to an instance of the [Placemark] class.
  static Placeinfo fromMap(dynamic message) {
    if (message == null) {
      throw ArgumentError('The parameter \'message\' should not be null.');
    }

    final Map<dynamic, dynamic> placemarkMap = message;

    return Placeinfo._(
      name: placemarkMap['name'] ?? '',
      country: placemarkMap['country'] ?? '',
      province: placemarkMap['province'] ?? '',
      city: placemarkMap['city'] ?? '',
      district: placemarkMap['district'] ?? '',
      township: placemarkMap['township'] ?? '',
      street: placemarkMap['street'] ?? '',
    );
  }

  /// Converts the [Placemark] instance into a [Map] instance that can be
  /// serialized to JSON.
  Map<String, dynamic> toJson() => {
        'name': name,
        'country': country,
        'province': province,
        'city': city,
        'district': district,
        'township': township,
        'street': street,
      };

  @override
  String toString() {
    return '''
      Name: $name, 
      Country: $country, 
      Province: $province, 
      City: $city, 
      District: $district,
      Township: $township,
      Street: $street''';
  }
}
